<?php
namespace Agent\Controller;

use Utuo\BackgroundController;

/**
 * 下属代理商管理
 * @author song
 */
class AgentController extends BackgroundController {
	
	protected function _initialize() {
		$this->setControllerTitle('下属代理商管理');
		$this->setActionTitle(array(
				'index' => '下属代理商列表',
		        'add' => '添加代理商',
				'modify' => '修改代理商',
				'recycle' => '回收站' ,
		));
		$this->setActionNav(array(
				'index',
				'recycle'
		));
	}
	
	/**
	 * 代理商列表
	 * @param unknown $is_deleted 是否已删除
	 */
	public function getLists($is_deleted=0) {
		$p = I("p", 1, 'intval');
		$pageSize = 10;
		/*查询条件*/
		$name = I('name', "");
		/*有条件查询*/
		$condition = array(
			"this0.is_deleted" => $is_deleted,
			"this0.parent_id" => $this->aid, // 下属代理商
		);
		$param = array();
		$name and $condition['this0.name'] = array('like', "%$name%") and $param['name'] = $name;
		/*搜索*/
		$customerListSql = M()->table("__AGENT__ this0")->join('LEFT JOIN __CN_PROVINCE__ this1 on this0.province=this1.code')
		                                                ->join('LEFT JOIN __CN_CITY__ this2 on this0.city=this2.code')
		                                                ->join('LEFT JOIN __CN_AREA__ this3 on this0.area=this3.code')
		                                                ->field("this0.*,this1.name as provinceName,this2.name as cityName,this3.name as areaName")
		                                                ->select(false);
		$customerLists = M()->table($customerListSql . " this0")->where($condition)->limit($pageSize)->page($p)->order('id desc')->select();
		$count = M()->table($customerListSql . " this0")->where($condition)->count();
		$this->assign("agentLists", $customerLists);
		$this->assign("param", $param);
		$this->showPage($count, $pageSize);
		$this->display();
	}
	
}